//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "OvernightIndex.h"
using namespace Cephei::QL::Indexes;
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Currency.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Period.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
using namespace Cephei::QL::Times;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Indexes::COvernightIndex::COvernightIndex (String^ familyName, UInt32 settlementDays, Cephei::QL::ICurrency^ currency, Cephei::QL::Times::ICalendar^ fixingCalendar, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ h) : CIborIndex(COvernightIndex::typeid)
{
    CCurrency^ _Ccurrency;
    CCalendar^ _CfixingCalendar;
    CDayCounter^ _CdayCounter;
    CYieldTermStructure^ _Ch;
    try
    {
#ifdef HANDLE
        _phOvernightIndex = NULL;
#endif
        std::string _familyName = (std::string)ValueHelper::Convert (familyName);
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays);
        _Ccurrency = safe_cast<CCurrency^> (currency);
        _Ccurrency->Lock();
        QuantLib::Currency& _currency = static_cast<QuantLib::Currency&> (_Ccurrency->GetReference ()); 
        _CfixingCalendar = safe_cast<CCalendar^> (fixingCalendar);
        _CfixingCalendar->Lock();
        QuantLib::Calendar& _fixingCalendar = static_cast<QuantLib::Calendar&> (_CfixingCalendar->GetReference ()); 
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (h))
        {
            _Ch = safe_cast<CYieldTermStructure^> (h->Value);
            _Ch->Lock();
        }
        Handle<QuantLib::YieldTermStructure>& _h = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (h) ? static_cast<Handle<QuantLib::YieldTermStructure>&> (_Ch->GetHandle ()) : Handle<QuantLib::YieldTermStructure>()); //1
        _ppOvernightIndex = new boost::shared_ptr<QuantLib::OvernightIndex> (new QuantLib::OvernightIndex ( _familyName,  _settlementDays,  _currency,  _fixingCalendar,  _dayCounter,  _h ));
        SetIborIndex (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppOvernightIndex));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccurrency != nullptr) _Ccurrency->Unlock();
        if (_CfixingCalendar != nullptr) _CfixingCalendar->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
        if (_Ch != nullptr) _Ch->Unlock();
    }
}
Cephei::QL::Indexes::COvernightIndex::COvernightIndex (boost::shared_ptr<QuantLib::OvernightIndex>& childNative, Object^ owner) : CIborIndex(COvernightIndex::typeid)
{
#ifdef HANDLE
	_phOvernightIndex = NULL;
#endif
	_ppOvernightIndex = &childNative;
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppOvernightIndex));
}
Cephei::QL::Indexes::COvernightIndex::COvernightIndex (QuantLib::OvernightIndex& childNative, Object^ owner) : CIborIndex(COvernightIndex::typeid)
{
#ifdef HANDLE
	_phOvernightIndex = NULL;
#endif
	_ppOvernightIndex = new boost::shared_ptr<QuantLib::OvernightIndex> (&childNative);
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppOvernightIndex));
    _OvernightIndexOwner = owner;
    _IborIndexOwner = owner;
}

Cephei::QL::Indexes::COvernightIndex::COvernightIndex (COvernightIndex^ copy) : CIborIndex(COvernightIndex::typeid)
{
#ifdef HANDLE
	_phOvernightIndex = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppOvernightIndex = new boost::shared_ptr<QuantLib::OvernightIndex> (copy->GetShared());
        _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppOvernightIndex));
    }
}
Cephei::QL::Indexes::COvernightIndex::COvernightIndex (System::Type^ t) : CIborIndex(COvernightIndex::typeid)
{
#ifdef HANDLE
	_phOvernightIndex = NULL;
#endif
	if (!t->IsSubclassOf(COvernightIndex::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Indexes::COvernightIndex::COvernightIndex (QuantLib::Handle<QuantLib::OvernightIndex>& childNative, Object^ owner)  : CIborIndex(COvernightIndex::typeid)
{
	_phOvernightIndex = &childNative;
	_ppOvernightIndex = &static_cast<boost::shared_ptr<QuantLib::OvernightIndex>>(childNative.currentLink());
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppOvernightIndex));
    _OvernightIndexOwner = owner;
}
Cephei::QL::Indexes::COvernightIndex::COvernightIndex (QuantLib::Handle<QuantLib::OvernightIndex> childNative)  : CIborIndex(COvernightIndex::typeid)
{
	_phOvernightIndex = &childNative;
	_ppOvernightIndex = &static_cast<boost::shared_ptr<QuantLib::OvernightIndex>>(childNative.currentLink());
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppOvernightIndex));
}
#endif
#ifdef STRUCT
Cephei::QL::Indexes::COvernightIndex::COvernightIndex (QuantLib::OvernightIndex childNative)  : CIborIndex(COvernightIndex::typeid)
{
#ifdef HANDLE
	_phOvernightIndex = NULL;
#endif
	_ppOvernightIndex = new boost::shared_ptr<QuantLib::OvernightIndex> (new QuantLib::OvernightIndex (childNative));
    _ppIborIndex = new boost::shared_ptr<QuantLib::IborIndex> (boost::dynamic_pointer_cast<QuantLib::IborIndex> (*_ppOvernightIndex));
}
#endif

Cephei::QL::Indexes::COvernightIndex::~COvernightIndex ()
{
    if (_ppOvernightIndex != NULL)
    {
	    delete _ppOvernightIndex;
        _ppOvernightIndex = NULL;
    }
}
Cephei::QL::Indexes::COvernightIndex::!COvernightIndex ()
{
    if (_ppOvernightIndex != NULL)
    {
	    delete _ppOvernightIndex;
    }
}
QuantLib::OvernightIndex& Cephei::QL::Indexes::COvernightIndex::GetReference ()
{
    if (_ppOvernightIndex == NULL) throw gcnew NativeNullException ();
	return **_ppOvernightIndex;
}
boost::shared_ptr<QuantLib::OvernightIndex>& Cephei::QL::Indexes::COvernightIndex::GetShared ()
{
    if (_ppOvernightIndex == NULL) throw gcnew NativeNullException ();
	return *_ppOvernightIndex;
}
QuantLib::OvernightIndex* Cephei::QL::Indexes::COvernightIndex::GetPointer ()
{
    if (_ppOvernightIndex == NULL) throw gcnew NativeNullException ();
	return &**_ppOvernightIndex;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::OvernightIndex>& Cephei::QL::Indexes::COvernightIndex::GetHandle ()
{
	if (_phOvernightIndex == NULL)
	{
		_phOvernightIndex = new Handle<QuantLib::OvernightIndex> (*_ppOvernightIndex);
	}
	return *_phOvernightIndex;
}
#endif
bool Cephei::QL::Indexes::COvernightIndex::HasNative () 
{
	return (_ppOvernightIndex != NULL);
}

Cephei::QL::Indexes::IIborIndex^ Cephei::QL::Indexes::COvernightIndex::Clone (Cephei::QL::Termstructures::IYieldTermStructure^ h)
{
    CYieldTermStructure^ _Ch;
    try
    {
        _Ch = safe_cast<CYieldTermStructure^> (h);
        _Ch->Lock();
        Handle<QuantLib::YieldTermStructure>& _h = static_cast<Handle<QuantLib::YieldTermStructure>&> (_Ch->GetHandle ()); 
    	boost::shared_ptr<QuantLib::IborIndex> _rv = (boost::shared_ptr<QuantLib::IborIndex>)(*_ppOvernightIndex)->clone ( _h );   
        Cephei::QL::Indexes::CIborIndex^ _nrv = gcnew Cephei::QL::Indexes::CIborIndex (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ch != nullptr) _Ch->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Indexes::IOvernightIndex^ Cephei::QL::Indexes::COvernightIndex_Factory::Create (String^ familyName, UInt32 settlementDays, Cephei::QL::ICurrency^ currency, Cephei::QL::Times::ICalendar^ fixingCalendar, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ h)
{
    return gcnew COvernightIndex ( familyName,  settlementDays,  currency,  fixingCalendar,  dayCounter,  h);
}
